﻿using System.Windows;
using SmartPeople.Framework.SecuritySpace;

namespace SecuritySpace.WPF.Demo
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        private ISecuritySpaceProviderFactory securitySpaceProviderFactory = new XPOProviderFactory();

        public Login()
        {
            InitializeComponent();
            InitializeSecurity();
        }

        private void InitializeSecurity()
        {
            ISecuritySpace securitySpace = securitySpaceProviderFactory.GetSecuritySpace();
            securitySpace.Clear();
            securitySpace.Intializer.Intialize(securitySpace);
        }


        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show("Please enter a username!", "Application Information", MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return;
            }

            if (string.IsNullOrEmpty(txtPassword.Password))
            {
                MessageBox.Show("Please enter a password!", "Application Information", MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return;
            }
            ISecuritySpace securitySpace = securitySpaceProviderFactory.GetSecuritySpace();
            IToken token = securitySpace.Users.Authenticate(txtUserName.Text, txtPassword.Password);
            if (token != null)
            {
                //MessageBox.Show(txtUserName.Text + " has sucessfully logged in!");
                Hide();
                MainMenu mainmenu = new MainMenu(token, securitySpace);
                mainmenu.Show();
            }
            else
            {
                MessageBox.Show(txtUserName.Text + " is an unauthorized user!");
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            txtPassword.Password = "";
            txtUserName.Text = "";
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
            Application.Current.Shutdown(0);
        }

        private void lblAdmin_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            txtPassword.Password = "admin123";
            txtUserName.Text =  "Administrator";
            
        }
    }
}